package com.collabscm.waf.solution.persistence.support;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.collabscm.waf.solution.persistence.DynamicQueryBreakdown;
import com.collabscm.waf.solution.persistence.WhereClauseBreakdown;
import com.collabscm.waf.solution.persistence.WhereCondition;
import com.collabscm.waf.solution.persistence.WhereJoinType;
import com.collabscm.waf.vo.ValueObject;

/**
 * 仅有And作为分割的Where子句
 *
 * @author Zeng Raoxing
 */
@Component
public class OnlyAndBreakdown extends ValueObject implements WhereClauseBreakdown {
	private static final long serialVersionUID = 1L;
	
	public OnlyAndBreakdown() {
		//通过Spring将此注册进去
		DynamicQueryBreakdown.registerWhereClauseBreakdown(WhereJoinType.AND, this);
	}

	@Override
	public List<WhereCondition> breakdown(String whereClause) {
//		String whereClause = dynamicQuery.substring(wherePos + 5);//过滤掉where之后的所有where子句
		String[] conditions = whereClause.split("@");
		List<WhereCondition> conditionList = new ArrayList<WhereCondition>(conditions.length);
		for(int i=0; i<conditions.length; i++) {
			String condition = conditions[i];
			int colonPos = condition.indexOf(":");//计算冒号的位置
			if(colonPos > 0) {
				int spacePos = condition.indexOf(" ", colonPos);//查找第一个空白区域
				String parameterName, continueJoinString;
				if(spacePos > 0) {
					parameterName = condition.substring(colonPos + 1, spacePos);
					continueJoinString = condition.substring(spacePos);
					condition = condition.substring(0, spacePos);
				} else {
					parameterName = condition.substring(colonPos + 1);
					continueJoinString = null;
				}
				conditionList.add(new SimpleWhereCondition(parameterName, condition, continueJoinString));
			}
		}
		return conditionList;
	}

}
